FROM debian:11.2

ARG PHP_VERSION

COPY dockerfiles/debian_php/sources.list /etc/apt/sources.list
RUN cat /etc/apt/sources.list
RUN apt-get clean && apt-get update --fix-missing
RUN apt-get install wget -y

RUN apt-get install git gcc automake autoconf libtool make wget build-essential \
    zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev \
    libreadline-dev libffi-dev curl libbz2-dev libxml2-dev \
    openssl libcurl4 libcurl4-gnutls-dev \
    libjpeg-dev libpng-dev libpq-dev libicu-dev \
    libfreetype6-dev libldap2-dev libxslt-dev libmariadb-dev-compat \
    libmariadb-dev libgmp-dev libgmp3-dev libxpm-dev pkg-config \
    libboost-all-dev iputils-ping vim \
    libjpeg62-turbo-dev libpng-dev patch libonig-dev -y

# 设置utf-8
RUN apt-get install apt-utils -y
RUN apt-get install locales -y
RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \
    locale-gen
ENV LANG en_US.UTF-8  
ENV LANGUAGE en_US:en  
ENV LC_ALL en_US.UTF-8

# 编译安装php
COPY dockerfiles/debian_php/php-${PHP_VERSION}.tar.gz /usr/local
RUN mkdir -p /usr/local/php \
    && cd /usr/local \
    && tar zxvf php-${PHP_VERSION}.tar.gz \
    && cd php-${PHP_VERSION} \
    && ./configure --prefix=/usr/local/php \
    --with-config-file-path=/etc \
    --with-mysql-sock --with-mysqli \
    --with-iconv --with-bz2 --with-curl \
    --with-cdb --enable-gd --with-jpeg \
    --with-freetype --with-gmp --with-mhash \
    --with-pdo-mysql --with-pear --enable-fpm \
    --enable-soap --enable-bcmath --enable-calendar \
    --enable-dom --enable-exif --enable-fileinfo \
    --enable-filter --enable-ftp --enable-json \
    --enable-session --enable-shmop --enable-simplexml \
    --enable-sockets --enable-sysvmsg --with-openssl \
    --with-openssl-dir --with-xsl --enable-sysvshm \
    --with-mhash --enable-fpm --enable-sysvsem \
    --enable-mbstring \
    && make clean && make && make install

# 创建php日志目录
RUN mkdir -p /var/log/php \
    && chmod -R 0755 /var/log/php

COPY dockerfiles/debian_php/php-fpm.conf /usr/local/php/etc/php-fpm.conf
COPY dockerfiles/debian_php/www.conf /usr/local/php/etc/php-fpm.d/www.conf
# 移动php.ini文件, 开启并修改php_errors.log的路径
RUN cp /usr/local/php-${PHP_VERSION}/php.ini-development /usr/local/php/etc/php.ini \
    && echo "error_log = /var/log/php/php_errors.log" >> /usr/local/php/etc/php.ini \
    && ln -s /usr/local/php/etc/php.ini /etc/php.ini \
    # && ln -s /usr/local/php/bin/php /usr/local/bin \
    # && ln -s /usr/local/php/bin/php-config /usr/local/bin \
    # && ln -s /usr/local/php/bin/phpize /usr/local/bin
    && ln -s /usr/local/php/bin/* /usr/local/bin/

RUN cd /usr/local \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# 编辑安装yaf
RUN pecl install yaf \
    && echo "extension=yaf.so" >> /usr/local/php/etc/php.ini \
    && echo "yaf.use_namespace=1" >> /usr/local/php/etc/php.ini \
    && echo "yaf.use_spl_autoload=1" >> /usr/local/php/etc/php.ini \
    && echo "yaf.cache_config=0" >> /usr/local/php/etc/php.ini \
    && echo "yaf.environ=\"develop_hsu\"" >> /usr/local/php/etc/php.ini

COPY dockerfiles/debian_php/phpredis-5.1.1.tar.gz /usr/local
RUN cd /usr/local \
    && mkdir -p phpredis \
    && tar -zxvf phpredis-5.1.1.tar.gz -C phpredis --strip-components=1 \
    && rm -f phpredis.tar.gz \
    && ( \
    cd phpredis \
    && /usr/local/php/bin/phpize \
    && ./configure \
    --with-php-config=/usr/local/php/bin/php-config \
    --enable-redis \
    && make \
    && make install \
    ) \
    && rm -rf phpreids \
    && sed -i "2i extension=redis.so" /usr/local/php/etc/php.ini

COPY dockerfiles/debian_php/swoole-src-4.4.14.tar.gz /usr/local
RUN cd /usr/local \
    && mkdir -p swoole \
    && tar -zxvf swoole-src-4.4.14.tar.gz -C swoole --strip-components=1 \
    && rm -f swoole-src-4.4.14.tar.gz \
    && ( \
    cd swoole \
    && /usr/local/php/bin/phpize \
    && ./configure --with-php-config=/usr/local/php/bin/php-config --enable-openssl \
    && make \
    && make install \
    ) \
    && rm -rf swoole \
    && sed -i "2i extension=swoole.so" /usr/local/php/etc/php.ini

RUN cd /usr/local \
    && git clone https://github.com/swoole/yasd.git \
    && (cd yasd \
    && /usr/local/php/bin/phpize --clean \
    && /usr/local/php/bin/phpize \
    && ./configure --with-php-config=/usr/local/php/bin/php-config \
    && make clean && make \
    && make install\
    ) \
    && sed -i "2i zend_extension=yasd" /usr/local/php/etc/php.ini \
    && sed -i "2i yasd.debug_mode=remote" /usr/local/php/etc/php.ini \
    && sed -i "2i yasd.remote_host=host.docker.internal" /usr/local/php/etc/php.ini \
    && sed -i "2i yasd.remote_port=9001" /usr/local/php/etc/php.ini

EXPOSE 9000 9501 9001